#include"menu.h"

/** Desenha o menu e permite a sua navegacao
 *
*/
Bool init()
{
	char* player=(char*)malloc(sizeof(char)*20);
	int score;
	menu escolha=PLAY;//predefinir a esolha selecionada como "Play"
	_go32_dpmi_seginfo old_kbd_irq;
	cleanscreen();
		int last_sc = 0;
		do
		{
			//Instala handler das interrupcoes do teclado
			install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
			
			if(escolha==PLAY)
				drawFrame("", LIGHTGRAY_BACK | RED_FOREG, WIDTH/2-6,6,12,3);
			else
				drawFrame("", LIGHTGRAY_BACK | BLUE_FOREG, WIDTH/2-6,6,12,3);
			printStringAt("Play",WIDTH/2-2,7,LIGHTGRAY_BACK);
			
			if(escolha==HIGH)
				drawFrame("", LIGHTGRAY_BACK | RED_FOREG, WIDTH/2-6,10,12,3);
			else
				drawFrame("", LIGHTGRAY_BACK | BLUE_FOREG, WIDTH/2-6,10,12,3);
			printStringAt("HighScores",WIDTH/2-5,11,LIGHTGRAY_BACK);
			
			if(escolha==EXIT)
				drawFrame("", LIGHTGRAY_BACK | RED_FOREG, WIDTH/2-6,14,12,3);
			else
				drawFrame("", LIGHTGRAY_BACK | BLUE_FOREG, WIDTH/2-6,14,12,3);
			printStringAt("Exit",WIDTH/2-2,15,LIGHTGRAY_BACK);
		
			drawFrame("Copy The Song", LIGHTGRAY_BACK | BLUE_FOREG, 25,3,30,18);
		
			do
			{
				while(scancode == last_sc);
				last_sc = scancode;
			}while (last_sc != UP && last_sc != DOWN && last_sc!=ENTER);
		
		
			if(last_sc==UP)
			{
				if(escolha==PLAY)
					escolha=EXIT;
				else 
					escolha--;
			}
			else if(last_sc==DOWN)
			{
				if(escolha==EXIT)
					escolha=PLAY;
				else 
					escolha++;
			}
			else if(last_sc==ENTER)
			{
				if(escolha==PLAY)
				{
					reinstall_asm_irq_handler(KBD_IRQ, &old_kbd_irq);
					player=choose_name();
					score=start_game(player);
					cleanscreen();
				}
				else if(escolha==HIGH)
				{
					reinstall_asm_irq_handler(KBD_IRQ, &old_kbd_irq);
					highscores();
					install_asm_irq_handler(KBD_IRQ, kbd_isr, &old_kbd_irq);
				}
			}
			reinstall_asm_irq_handler(KBD_IRQ, &old_kbd_irq);
		}while(escolha!=EXIT || last_sc!=ENTER); //Se estiver em sair e carregar Enter
	
	return false;
}

/** O Jogador insere o seu nome dentro de uma caixa de texto
 *
*/
char* choose_name()
{
	char* name=(char*)malloc(sizeof(char)*20);
	cleanscreen();
	drawFrame("Write your name", LIGHTGRAY_BACK | BLUE_FOREG, 25,3,20,3);
	gotoxy(27,5);
	scanf("%s",name);
	printf("%s\n",name);
	return name;
}

/** Imprime no ecra as melhores pontuacoes
 *
*/
void highscores()
{
	char * nomes[10]; 
	int pontos[10] , n;
	
	n=load(nomes,pontos);
	
	cleanscreen();
	
	drawFrame("Highscores", LIGHTGRAY_BACK | BLUE_FOREG, 26,4,27,2+n);
	int i;
	for(i=0;i<n;i++)
	{
		printStringAt(nomes[i],27,5+i,LIGHTGRAY_BACK);
		if(pontos[i]==0)
			printIntAt(pontos[i],51,5+i,LIGHTGRAY_BACK);
		else if(pontos[i]<1000)
			printIntAt(pontos[i],49,5+i,LIGHTGRAY_BACK);
		else
			printIntAt(pontos[i],48,5+i,LIGHTGRAY_BACK);
	}
	getchar();
	cleanscreen();
}
